<?php
namespace XtwsBack\Controller;
use Think\Controller;

class GroupOrderController extends CommonController {

    //首页
    public function index(){
        $order = M('shop_spellgroup_order');
        $count = $order->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,100);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $list = $order->table(C('DB_PREFIX')."shop_spellgroup_order as o")
            ->join(C('DB_PREFIX')."shop_spellgroup_copy as c ON c.id = o.c_id")
            ->field('o.*,c.sg_id,c.num')
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order('o.addtime desc')
            ->select();
        foreach($list as $k => $v) {// 查询拼团商品信息，拼团的商家或者农户的信息
            $list[$k]['nickname'] = M('member')->where('id=' . $v['u_id'])->getField('nickname');
            //$list[$k]['goods_info'] 根据sg_id查询拼团商品信息
            $list[$k]['goods_info'] = M('shop_spellgroup')->where('id=' . $v['sg_id'])->field('u_id,endtime,s_id,type,title,h_imgs,actual_price,price,min_num')->find();
            // 处理主图
            $list[$k]['goods_info']['h_imgs'] = isMorePic($list[$k]['goods_info']['h_imgs']);
            // 查询商家或农户的姓名与头像
            $list[$k]['store_info'] = getMember($list[$k]['goods_info']['u_id'], 'realname,nickname,avatar');
            if ($list[$k]['goods_info']['s_id'] != 0) {//社区拼团-查询店铺名称
                $list[$k]['store_info']['storename'] = getStore($list[$k]['goods_info']['s_id']);
            } else {//乡村拼团-查询农户所在的村庄
                $list[$k]['store_info']['storename'] = M('member_farmers')->where('u_id = ' . $list[$k]['goods_info']['u_id'])->getField('village');
            }// end if
        }//end foreach
        rsort($list);//倒序排列
//        array_values($list);//重置键值
//        dump($list);
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出

        //加载视图
        $this->display();
    }//end fun


    // 订单详情
    public function orderinfo(){
        $id = intval(I('id'));
        $map['id'] = $id;
        $info = getInfo('shop_spellgroup_order',$map);
        if($info){
            $member =  getMember($info['u_id'],$field='nickname,avatar,realname,mobile');
            // 查询拼团副本中拼团商品id,以及已拼商品的份数
            $c_info =  getInfo('shop_spellgroup_copy',array('id'=>$info['c_id']),'sg_id,num');
            // 根据sg_id查询拼团商品信息
            $goods_info = getInfo('shop_spellgroup',array('id'=>$c_info['sg_id']),'u_id,s_id,type,title,endtime,h_imgs,actual_price,price,min_num');
            $goods_info['h_imgs'] = explode(",",$goods_info['h_imgs']);
            $goods_info['endtime'] = $goods_info['endtime']-time();    //处理拼团失效时间
            // 查询店铺老板或者农户的头像、昵称
            $store_info = getMember($goods_info['u_id'],'nickname,avatar');
            if($goods_info['s_id']!=0){// 社区拼团-查询店铺名称
                $goods_info['storename'] = getStore($goods_info['s_id']);
            }else{// 乡村拼团查询农户乡村名称
                $goods_info['storename'] = M('member_farmers')->where('u_id = '.$goods_info['u_id'])->getField('village');
            }// end if

            // 获取订单地址
            $addr_info = M('shop_community')->where(array('id' => $info['comm_id'] ))->field('title,a_id_1,a_id_2,a_id_3')->find();
            $addr_info['pro'] = M('area')->where('id='.$addr_info['a_id_1'])->getField('name');
            $addr_info['city'] = M('area')->where('id='.$addr_info['a_id_2'])->getField('name');
            $addr_info['area'] = M('area')->where('id='.$addr_info['a_id_3'])->getField('name');

            $this -> assign('info',$info);              // 订单信息
            $this -> assign('member',$member);          // 购买人
            $this -> assign('c_info',$c_info);          // 拼团情况
            $this -> assign('goods_info',$goods_info);  // 拼团商品信息
            $this -> assign('store_info',$store_info);  // 店铺老板信息
            $this -> assign('addr_info',$addr_info);    // 地址
            $this->display();
        }else{
            $this->error('没有记录');
        }// end if

    }// end fun orderinfo()




}//end class